package firewing.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.logging.Logger;

public class QueryFeeder {

	private static final String INPUT_FOLDER = "data/";

	private static final String URL_BASE = "http://localhost:8888/GoogleSearchAdsExtractor?query=";

	private static final Logger LOG = Logger.getLogger(QueryFeeder.class
			.getName());

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// list all input files

		File inputFolder = new File(INPUT_FOLDER);

		String[] inputFiles = inputFolder.list();

		for (String file : inputFiles) {
			LOG.info("Processing new file: " + file);

			try {
				BufferedReader in = new BufferedReader(new FileReader(
						INPUT_FOLDER + file));
				for (String s = in.readLine(); s != null; s = in.readLine()) {
					try {
						URL url = new URL(URL_BASE
								+ URLEncoder.encode(s, "UTF-8"));
						HttpURLConnection conn = (HttpURLConnection) url
								.openConnection();

						conn.setAllowUserInteraction(false);
						conn.setDoInput(true);

						conn.connect();
					} catch (Exception e) {
						e.printStackTrace();
						continue;
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
